initial commit
[AXOD IR_433MHz Shield] / FIRMWARE / rx433.c
1 // -----------------------------------------------------------------------------------------------
2 // ------------------------------- radio 433MHz receiver and decoder -----------------------------
3 // -----------------------------------------------------------------------------------------------
4
5 extern void put_num( uint64_t num );
6
7 #define SHORT_LOW       0
8 #define SHORT_HIGH      1
9 #define LONG_LOW        2
10 #define LONG_HIGH       3
11
12 #define         RX433_BUFF      64
13 #define         RX_TRESHOLD     5       // 16mHz crystall, see timer in UART routine
14
15 #define         IDLE            0
16 #define         READ            1
17 #define         DONE            2
18 #define         FAIL            3
19
20 #define         MIN_PACKET_LEN  40      // raw data, for filtering
21 #define         MAX_PACKET_LEN  55      // raw data, for filtering
22
23 uint8_t         RXBuff[ RX433_BUFF ];
24
25 uint16_t        cnt = 0;
26 uint8_t         rx_done = IDLE;
27
28 uint8_t         current_rx_bit = 0, prev_rx_bit = 0, keep_state = 0, current_state = 0;
29
30 // ------------------------------ tripe-bit decode -----------------------------------------------
31
32 uint8_t triple_bit_433( void ){
33
34         uint8_t a = 0;
35
36         for ( a = 0; a < 12; a++ ){
37
38                 switch( *(uint32_t *)(&RXBuff[a*4]) ){
39
40                         case 0x00030201: RXBuff[ a ] = 'F'; break;      // 1230
41                         case 0x02010201: RXBuff[ a ] = '0'; break;      // 1212
42                         case 0x00030003: RXBuff[ a ] = '1'; break;      // 3030
43                         case 0x02010003: RXBuff[ a ] = 'X'; break;      // 3012
44
45                         default : return 0; break;                      // wrong sequence
46                 }
47         }
48
49         return a;
50 }
51
52 // ------------------------------- manchester decode ---------------------------------------------
53
54 uint8_t manchester_433( void ){
55
56         uint8_t newCnt = 0;
57         uint8_t a = 0;
58
59         for ( a = 0; a < RX433_BUFF; a++ ){
60
61                 if ( RXBuff[a+1] == 5 ) break;
62
63                 if (a != 0) newCnt++;
64
65                 if ( RXBuff[a] == SHORT_LOW && RXBuff[a+1] == SHORT_HIGH )      { RXBuff[ newCnt ] = 1; a++; continue; }
66                 if ( RXBuff[a] == SHORT_LOW && RXBuff[a+1] == LONG_HIGH )       { RXBuff[ newCnt ] = 1; continue; }
67                 if ( RXBuff[a] == LONG_LOW && RXBuff[a+1] == SHORT_HIGH )       { RXBuff[ newCnt ] = 1; a++; continue; }
68                 if ( RXBuff[a] == LONG_LOW && RXBuff[a+1] == LONG_HIGH )        { RXBuff[ newCnt ] = 1; continue; }
69
70                 if ( RXBuff[a] == SHORT_HIGH && RXBuff[a+1] == SHORT_LOW )      { RXBuff[ newCnt ] = 0; a++; continue; }
71                 if ( RXBuff[a] == SHORT_HIGH && RXBuff[a+1] == LONG_LOW )       { RXBuff[ newCnt ] = 0; continue; }
72                 if ( RXBuff[a] == LONG_HIGH && RXBuff[a+1] == SHORT_LOW )       { RXBuff[ newCnt ] = 0; a++; continue; }
73                 if ( RXBuff[a] == LONG_HIGH && RXBuff[a+1] == LONG_LOW )        { RXBuff[ newCnt ] = 0; continue; }
74
75                 break;
76         }
77
78         for ( a = newCnt; a < RX433_BUFF; a++ ) RXBuff[ a ] = 7;
79
80         return newCnt;
81
82 }
83
84 // ------------------------------- capture raw data -------------------------------------------
85 // ------------------------------- for using inside the Timer ISR -----------------------------
86
87 void read433_handler( void ){
88
89 //      current_rx_bit = (PINB & 0b00000100) >> 2;
90         current_rx_bit = (PINA & (1 << RX433)) >> RX433;
91
92         if (prev_rx_bit == current_rx_bit) {
93                 keep_state ++; 
94                 if ( current_rx_bit == 0 && keep_state > RX_TRESHOLD * 4 && rx_done == READ && cnt > MIN_PACKET_LEN && cnt < MAX_PACKET_LEN ) { rx_done = DONE; RXBuff[ cnt ] = 5; }    // end of packet
95         } else {
96
97                         if ( prev_rx_bit == 0 && keep_state > RX_TRESHOLD * 4 && rx_done == IDLE ) { cnt = 0; rx_done = READ; } // begin of packet 
98
99
100                         if ( rx_done == READ ) {
101
102                                 if ( prev_rx_bit == 0 ){                
103                                         if ( keep_state < RX_TRESHOLD ) current_state = SHORT_LOW; else current_state = LONG_LOW;  
104                                 } else {                        
105                                         if ( keep_state < RX_TRESHOLD ) current_state = SHORT_HIGH; else current_state = LONG_HIGH;  
106                                 } 
107
108                                 if (cnt < RX433_BUFF ) {
109                                         if (cnt != 0) RXBuff[ cnt-1 ] = current_state; 
110                                         cnt++;
111                                 } else { 
112                                         rx_done = IDLE;         // end of buffer
113                                         RXBuff[ cnt ] = 5;
114                                         cnt = 0; 
115                                 }
116
117                         }
118
119 /*
120                         // treshold test : 16mHz, output: 13 11 3 3 12 10 4 3 11 11 4 2 12 2 12 3 11 11 4 2 12 11 3 3 11 11 4 2 12 11 3 3 
121
122                         if (cnt < RX433_BUFF ) {
123                                 RXBuff[ cnt++ ] = keep_state; 
124                         } else { 
125                                 rx_done = DONE;         // end 
126                                 cnt = 0; 
127                         }
128 */
129                 keep_state = 0;
130         }
131         prev_rx_bit = current_rx_bit;
132 }
133
Contact me: dev (at) shalnoff (dot) com
PGP fingerprint: A6B8 3B23 6013 F18A 0C71 198B 83D8 C64D 917A 5717